package fm.dongman.modellib.remote;

import android.support.annotation.NonNull;
import android.util.Log;

import org.json.JSONObject;

import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.contract.SearchContract;
import fm.dongman.animefm.contractlib.model.SearchDataModel;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.local.DataOperationHelper;

/**
 * 搜索
 * Created by KUIGE on 2017/10/26.
 */

public class SearchRemoteDataSource implements SearchContract.ISearchDataSource {
    private static SearchRemoteDataSource SINGLETON; // 实例

    private RemoteOperationHelper mOperation;
    private boolean isRemote = true;


    private SearchRemoteDataSource() {
        mOperation = RemoteOperationHelper.getOperationHelper();
    }

    public static SearchRemoteDataSource getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new SearchRemoteDataSource();
        }
        return SINGLETON;
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super SearchDataModel> callback) {
        if (isRemote) {
            SearchDataModel model = new SearchDataModel();
            List<String> slist = DataOperationHelper.getInstance().obtainSearchKeys();
            model.setHistorySearchList(slist);
            JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SEARCH_REMOTE + "page=1&size=6");
            if (object != null) {
                List<String> list = mOperation.downSearchHotWords(object);
                model.setHotSearchList(list);
                callback.onDataLoaded(model);
            } else {
                callback.onDataLoadFiled();
            }
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(SearchDataModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }

    @Override
    public void clearHistorySearch(LoadSourceCallback<? super String> loadSourceCallback) {
    }
}
